package com.stand.hardcode.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.stand.hardcode.entity.StandUser;
import com.stand.hardcode.exception.UsernameTakenException;
import com.stand.hardcode.manager.UserManager;
import com.stand.hardcode.util.ParamsVerifier;
import com.stand.hardcode.util.RequestAttributes;

public class AdminPanelServlet extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		if(UserManager.IsLoggedIn(req)){
			StandUser u = (StandUser)req.getSession().getAttribute(RequestAttributes.CURRENT_USER);
			if(u.isAdmin()){
				
				try {
					req.getRequestDispatcher("adminpanel.jsp").forward(req, resp);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				resp.sendRedirect("/profile");
			}
		}else{
			resp.sendRedirect("/home");
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		if(UserManager.IsLoggedIn(req)){
			StandUser u =(StandUser)req.getSession().getAttribute(RequestAttributes.CURRENT_USER);
			if(u.isAdmin()){
				
				String search = (String)req.getParameter(RequestAttributes.SEARCH_EMAIL);
				if(ParamsVerifier.ParamsNotNull(search)){
					
					StandUser s = UserManager.RetrieveUser(KeyFactory.createKey(StandUser.class.getSimpleName(), search));
					if(s != null){
						req.setAttribute(RequestAttributes.HAS_SEARCH_RESULTS, RequestAttributes.YES);
						req.setAttribute(RequestAttributes.SEARCH_EMAIL, s.getEmail());
						req.setAttribute(RequestAttributes.SEARCH_ACTIVE_STATUS, s.isActive());
						req.setAttribute(RequestAttributes.SEARCH_NICKNAME, s.getName());
						
						try {
							req.getRequestDispatcher("adminpanel.jsp").forward(req, resp);
						} catch (ServletException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else{
						req.setAttribute(RequestAttributes.HAS_SEARCH_RESULTS, RequestAttributes.YES);
						req.setAttribute(RequestAttributes.SEARCH_EMAIL, search);
						req.setAttribute(RequestAttributes.SEARCH_ACTIVE_STATUS, "-");
						req.setAttribute(RequestAttributes.SEARCH_NICKNAME, "-");
						resp.sendRedirect("/adminpanel");
						try {
							req.getRequestDispatcher("adminpanel.jsp").forward(req, resp);
						} catch (ServletException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				
			}else{
				resp.getWriter().println("not admin, why are u here");
			}
		}else{
			resp.getWriter().println("not logged in");
		}
	}
}
